package ru.spbau.design.snmp.android.viewer.view;

import java.util.HashMap;
import java.util.Map;

import ru.spbau.design.snmp.android.viewer.R;
import ru.spbau.design.snmp.android.viewer.model.IPAddress;
import ru.spbau.design.snmp.android.viewer.model.NetInterface;
import ru.spbau.design.snmp.android.viewer.model.Network;
import ru.spbau.design.snmp.android.viewer.model.Router;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TableRow;
import android.widget.TextView;

class RouterTab implements Listener {
	ExpandableListView view;
	RouterAdapter adapter;
	RoutersViewClickListener listener;
	ViewerActivity.Accessor accessor;

	public RouterTab(ViewerActivity.Accessor accessor) {
		this.accessor = accessor;

		this.view = (ExpandableListView) this.accessor.context().findViewById(
				R.id.routersView);
		this.listener = new RoutersViewClickListener();
		this.adapter = new RouterAdapter();

		this.view.setAdapter(this.adapter);
		this.view.setOnChildClickListener(this.listener);

		this.accessor.context().subscribe(this.adapter);
		this.accessor.context().subscribe(this);
	}

	public ExpandableListView getView() {
		return view;
	}

	public Rev getRouterPos(NetInterface iface) {
		return this.adapter.getPos(iface);
	}

	public void onConfigurationChanged() {
		this.view.invalidateViews();
	}

	// --------------------------------------------------------------------------------

	private class RouterAdapter extends AdapterBase implements Listener {
		Map<NetInterface, Rev> rev;
		int maxChildren;

		public RouterAdapter() {
			this.rev = new HashMap<NetInterface, Rev>();
		}

		public Rev getPos(NetInterface iface) {
			return this.rev.get(iface);
		}

		public int getPos(Router router) {
			return this.rev.get(router.getInterfaces().get(0)).getGroup();
		}

		public View getGroupView(int position, boolean isExpanded,
				View convertView, ViewGroup parent) {
			/*
			 * System.out.println("Displaying router" +
			 * accessor.network().getRouters().get(position) .getDescription());
			 */

			TextView tv = (TextView) convertView;
			if (tv == null) {
				tv = new TextView(accessor.context());
			}

			tv.setText(accessor.network().getRouters().get(position)
					.getDescription());
			tv.setMinHeight(50);
			tv.setPadding(0, 10, 0, 0);
			return tv;
		}

		public boolean isEmpty() {
			if (accessor.network() == null) {
				return false;
			}

			return accessor.network().getRouters().isEmpty();
		}

		public View getChildView(int groupPosition, int childPosition,
				boolean isExpanded, View convertView, ViewGroup viewGroup) {

			View v = convertView;

			if (v == null) {
				LayoutInflater vi = (LayoutInflater) accessor.context()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.interfaceview, null);
			}

			TextView tv;

			NetInterface iface = accessor.network().getRouters().get(
					groupPosition).getInterfaces().get(childPosition);
			tv = (TextView) v.findViewById(R.id.address);
			tv.setText(iface.getAddress().getAddressStr());

			tv = (TextView) v.findViewById(R.id.inBytes);
			tv.setText(String.valueOf(iface.getInBytes()));

			tv = (TextView) v.findViewById(R.id.outBytes);
			tv.setText(String.valueOf(iface.getOutBytes()));

			TableRow r = (TableRow) v.findViewById(R.id.nameRow);

			if (iface.getName().length() == 0) {
				r.setVisibility(View.INVISIBLE);
			} else {
				tv = (TextView) v.findViewById(R.id.dnsname);
				tv.setText(iface.getName());
			}

			return v;
		}

		public int getGroupCount() {
			/*
			 * System.out.println("Getting router count " +
			 * String.valueOf(accessor.network().getRouters().size()));
			 */

			if (accessor.network() == null) {
				return 0;
			}

			return accessor.network().getRouters().size();
		}

		public int getChildrenCount(int groupPosition) {
			if (accessor.network() == null) {
				return 0;
			}

			return accessor.network().getRouters().get(groupPosition)
					.getInterfaces().size();
		}

		public long getCombinedChildId(long groupId, long childId) {
			return groupId * this.maxChildren + (childId + 1);
		}

		public long getCombinedGroupId(long groupId) {
			return groupId * this.maxChildren;
		}

		public void onConfigurationChanged() {
			this.rev.clear();

			int nRouter = 0;
			this.maxChildren = 0;
			while (nRouter < accessor.network().getRouters().size()) {
				Router router = accessor.network().getRouters().get(nRouter);

				int nIface = 0;
				while (nIface < router.getInterfaces().size()) {
					this.rev.put(router.getInterfaces().get(nIface), new Rev(
							nRouter, nIface));
					nIface++;

					if (this.maxChildren < router.getInterfaces().size()) {
						this.maxChildren = router.getInterfaces().size();
					}
				}

				nRouter++;
			}

			this.maxChildren++;
		}
	}

	// --------------------------------------------------------------------------------

	private class RoutersViewClickListener implements
			ExpandableListView.OnChildClickListener {

		public RoutersViewClickListener() {
		}

		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {

			accessor.context().tabs.setCurrentTabByTag("Subnets");

			NetInterface iface = accessor.network().getRouters().get(
					groupPosition).getInterfaces().get(childPosition);
			IPAddress ifAddr = iface.getAddress();
			Rev vPos = accessor.getSubnetPos(iface.getSubNet().getNetNodeByIP(
					ifAddr).getItem());

			if (vPos == null) {
				System.out.println("FFFFUUUUUU!");
			} else {
				accessor.tabs().setCurrentTabByTag("Subnets");
				accessor.subnetsView().expandGroup(vPos.getGroup());
				accessor.subnetsView().setSelectedChild(vPos.getGroup(),
						vPos.getChild(), true);
			}

			return true;
		}
	}
}
